﻿using System;
using System.Deployment.Application;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using SGLLv2.UI.Properties;

namespace SGLLv2.UI
{
    public partial class MDIParent : Form
    {
        // Declare Global Variables
        private readonly Stopwatch stopwatch;

        // Declare Enumeration Variables
        public enum MessageType
        {
            Successful,     //0
            Information,    //1
            Executing,      //2
            Warning,        //3
            Error,          //4
            Question        //5
        }

        // Form
        public MDIParent()
        {
            InitializeComponent();

            // Initialize the Stopwatch
            stopwatch = new Stopwatch();

            // Initialize the Tool Strip Status Labels
            toolStripStatusLabelVersion.Text = ApplicationDeployment.IsNetworkDeployed ? ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString() : Settings.Default.AppDevVersion;
            toolStripStatusLabelWorkstation.Text = Environment.MachineName;
            toolStripStatusLabelUsername.Text = Environment.UserName;
            toolStripStatusLabelDomain.Text = Environment.UserDomainName;
            toolStripStatusLabelMessages.Text = string.Empty;
        }
        private void MDIParent_Load(object sender, EventArgs e)
        {
            try
            {
                // Begin
                Begin();

                // Coding
                // Set form title
                Text = Settings.Default.AppFullName;

                // Set the number of calls and complaints of the day.
                UpdateTodayCallsComplaints();

                // Loop through all of the form's controls looking
                // for the control of type MdiClient.
                foreach (Control control in Controls)
                {
                    try
                    {
                        // Attempt to cast the control to type MdiClient.
                        MdiClient mdiParent = (MdiClient)control;

                        // Set the BackColor of the MdiClient control.
                        mdiParent.BackColor = Color.White;
                        mdiParent.BackgroundImage = Resources.logo1;
                    }
                    catch (InvalidCastException)
                    {
                        // Catch and ignore the error if casting failed.
                    }
                }

                // Set up Culture
                //Thread.CurrentThread.CurrentCulture = new CultureInfo(Settings.Default.CurrentCulture);

                // Reseed Calls and Complaints
                queriesTableAdapter.ReseedCalls();
                queriesTableAdapter.ReseedComplaints();

                // Let the User know
                SetMessage(MessageType.Information, Resources.MessageSuccessfulStartup);

                // Log File
            }
            catch (Exception exception)
            {
                // Report Error.
                SetMessage(MessageType.Error, exception.Message);
                Utilities.ExceptionManager(exception);
            }
            finally
            {
                // End
                End();
            }
        }
        private void MDIParent_MdiChildActivate(object sender, EventArgs e)
        {
            // Reset environment
            toolStripMenu.BindingSource = null;
            toolStripMenuItemNew.Enabled = toolStripButtonNew.Enabled = false;
            toolStripStatusLabelShowAllCalls.Visible = toolStripStatusLabelNumberCallsFiltered.Visible = false;
            toolStripStatusLabelShowAllComplaints.Visible = toolStripStatusLabelNumberComplaintsFiltered.Visible = false;
            toolStripMenuItemClear.Enabled = toolStripButtonClear.Enabled = false;
            //toolStripMenuItemPrint.Enabled = toolStripButtonPrint.Enabled = false;

            // Declare Local Variables
            IMethods methods = (IMethods)ActiveMdiChild;

            // Validations
            if (methods != null)
                methods.SetControlBox();
        }

        // Tool Strip Menu Items
        private void toolStripMenuItemCalls_Click(object sender, EventArgs e)
        {
            // Initialize a new instance of calls
            FormCalls formCalls = new FormCalls();
            LoadForm(formCalls);

            // Set the appropriate controls.
            toolStripMenuItemClear.Enabled = toolStripButtonClear.Enabled = true;
        }
        private void toolStripMenuItemComplaints_Click(object sender, EventArgs e)
        {
            // Initialize a new instance of complaints.
            var formComplaints = new FormComplaints();
            LoadForm(formComplaints);

            // Set the appropriate controls.
            toolStripMenuItemClear.Enabled = toolStripButtonClear.Enabled = true;
        }
        private void toolStripMenuItemServices_Click(object sender, EventArgs e)
        {
            // Initialize a new instance of complaints.
            var formServices = new FormServices();
            LoadForm(formServices);

            // Set the appropriate controls.
            toolStripMenuItemClear.Enabled = toolStripButtonClear.Enabled = true;
        }
        private void toolStripMenuItemMessengers_Click(object sender, EventArgs e)
        {
            // Initialize a new instance of complaints.
            var formMessengers = new FormMessenger();
            LoadForm(formMessengers);

            // Set the appropriate controls.
            toolStripMenuItemClear.Enabled = toolStripButtonClear.Enabled = true;
        }
        private void toolStripMenuItemOptions_Click(object sender, EventArgs e)
        {
            // Initialize a new instance of complaints.
            var formSettings = new FormSettings();
            LoadForm(formSettings);

            // Set the appropriate controls.
            toolStripMenuItemNew.Enabled = toolStripButtonNew.Enabled = true;
        }
        private void toolStripMenuItemNew_Click(object sender, EventArgs e)
        {
            // Declare Local Variables
            IMethods methods = (IMethods)ActiveMdiChild;

            // Validations
            if (methods != null)
                methods.New();
        }
        private void toolStripMenuItemSave_Click(object sender, EventArgs e)
        {
            // Declare Local Variables
            IMethods methods = (IMethods)ActiveMdiChild;

            // Validations
            if (methods != null)
                methods.Save();
        }
        private void toolStripMenuItemClear_Click(object sender, EventArgs e)
        {
            // Declare Local Variables
            IMethods methods = (IMethods)ActiveMdiChild;

            // Validations
            if (methods != null) methods.ClearForm();
        }
        private void toolStripMenuItemPrint_Click(object sender, EventArgs e)
        {
            // Declare Local Variables
            IMethods methods = (IMethods) ActiveMdiChild;

            // Validations
            if (methods != null)
                methods.Print();
        }
        private void toolStripMenuItemExport_Click(object sender, EventArgs e)
        {
            // Declare Local Variables
            IMethods methods = (IMethods)ActiveMdiChild;

            // Validations
            if (methods != null)
                methods.Export();
        }
        private void toolStripMenuItemClose_Click(object sender, EventArgs e)
        {
            var form = ActiveMdiChild;
            if (form != null)
                form.Close();
        }
        private void toolStripMenuItemCloseAll_Click(object sender, EventArgs e)
        {
            foreach (var form in MdiChildren)
                form.Close();
            toolStripMenuItemCall.Enabled = toolStripMenuItemComplaints.Enabled = toolStripMenuItemSettings.Enabled = true;
        }
        private void toolStripMenuItemExit_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void toolStripMenuItemToolBar_Click(object sender, EventArgs e)
        {
            toolStripMenu.Visible = toolBarToolStripMenuItem.Checked;
        }
        private void toolStripMenuItemStatusBar_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }
        private void toolStripMenuItemModes_Click(object sender, EventArgs e)
        {
            if (toolStripMenuItemModeI.Visible || toolStripMenuItemModeII.Visible || toolStripMenuItemModeIII.Visible) return;

            // Declare Local Variables
            var toolStripMenuItem = (ToolStripMenuItem) sender;
            
            switch ((Global.FormViewModes)toolStripMenuItem.Tag)
            {
                case Global.FormViewModes.ModeI:
                    toolStripMenuItemModeII.Checked = toolStripMenuItemModeIII.Checked = false;
                    break;
                case Global.FormViewModes.ModeII:
                    toolStripMenuItemModeI.Checked = toolStripMenuItemModeIII.Checked = false;
                    break;
                case Global.FormViewModes.ModeIII:
                    toolStripMenuItemModeI.Checked = toolStripMenuItemModeII.Checked = false;
                    break;
            }

            // Declare Local Variables
            IMethods methods = (IMethods)ActiveMdiChild;

            // Validations
            if (methods != null) methods.ChangeFormView((Global.FormViewModes)toolStripMenuItem.Tag);
        }

        // Tool Strip Button
        private void toolStripButtonCalls_Click(object sender, EventArgs e)
        {
            toolStripMenuItemCalls_Click(null, null);
        }
        private void toolStripButtonComplaints_Click(object sender, EventArgs e)
        {
            toolStripMenuItemComplaints_Click(null, null);
        }
        private void toolStripButtonServices_Click(object sender, EventArgs e)
        {
            toolStripMenuItemServices_Click(null, null);
        }
        private void toolStripButtonOptions_Click(object sender, EventArgs e)
        {
            toolStripMenuItemOptions_Click(null, null);
        }
        private void toolStripButtonNew_Click(object sender, EventArgs e)
        {
            toolStripMenuItemNew_Click(null, null);
        }
        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            toolStripMenuItemSave_Click(null, null);
        }
        private void toolStripButtonClear_Click(object sender, EventArgs e)
        {
            toolStripMenuItemClear_Click(null, null);
        }
        private void toolStripButtonPrint_Click(object sender, EventArgs e)
        {
            toolStripMenuItemPrint_Click(null, null);
        }

        // Tool Strip Status Bar
        private void toolStripStatusLabelShowAllCalls_Click(object sender, EventArgs e)
        {
            // Declare Local Variables
            IMethods methods = (IMethods)ActiveMdiChild;

            // Validations
            if (ActiveMdiChild != null && (methods != null &&  ActiveMdiChild.Name.Equals("FormCalls")))
                methods.ClearFilter();

            // Hide method to prevent double clicking.
            toolStripStatusLabelShowAllCalls.Visible = false;
        }
        private void toolStripStatusLabelShowAllComplaints_Click(object sender, EventArgs e)
        {
            // Declare Local Variables
            IMethods methods = (IMethods)ActiveMdiChild;

            // Validations
            if (ActiveMdiChild != null && (methods != null && ActiveMdiChild.Name.Equals("FormComplaints")))
                methods.ClearFilter();

            // Hide method to prevent double clicking.
            toolStripStatusLabelShowAllComplaints.Visible = false;
        }

        // General Propose Functions.
        private void LoadForm(Form form)
        {
            form.MdiParent = this;
            form.WindowState = FormWindowState.Maximized;
            form.Show();
        }
        /// <summary>
        /// Locks the main form while processing any operations
        /// </summary>
        public void Begin()
        {
            stopwatch.Start();
            Cursor = Cursors.WaitCursor;
            SetMessage(MessageType.Executing, Resources.MessageExecutingProcess);
            //Enabled = false;
        }
        /// <summary>
        /// Unlock the main form.
        /// </summary>
        public void End()
        {
            stopwatch.Stop();
            toolStripStatusLabelElapsedTime.Text = string.Format(stopwatch.Elapsed.ToString());
            stopwatch.Reset();
            Cursor = Cursors.Default;
            //Enabled = true;
        }
        /// <summary>
        /// Update the number of Calls and Complaints of the day.
        /// </summary>
        public void UpdateTodayCallsComplaints()
        {
            // Update the number of calls.
            toolStripStatusLabelNumberCalls.Text = string.Format(Settings.Default.CallsQuantity, queriesTableAdapter.GetTodayCalls());

            // Update the number of complaints.
            toolStripStatusLabelNumberComplaints.Text = string.Format(Settings.Default.ComplaintsQuantity, queriesTableAdapter.GetTodayComplaints());
        }
        /// <summary>
        /// Reset the Tool Strip Status Label Message
        /// </summary>
        public void SetMessage()
        {
            toolStripStatusLabelMessages.Text = string.Empty;
            toolStripStatusLabelMessages.Image = null;
        }
        /// <summary>
        /// Display to the user the activities occurred inside the program
        /// </summary>
        /// <param name="messageType">Type of the Message to Display</param>
        /// <param name="message">Content of the Message</param>
        public DialogResult SetMessage(MessageType messageType, string message)
        {
            // Declare Local Variables
            var result = DialogResult.None;
            
            toolStripStatusLabelMessages.Text = message;
            switch (messageType)
            {
                case MessageType.Executing:
                    toolStripStatusLabelMessages.Image = Resources.executing;
                    break;
                case MessageType.Successful:
                    MessageBox.Show(message, Settings.Default.AppFullName, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    toolStripStatusLabelMessages.Image = Resources.successful;
                    break;
                case MessageType.Information:
                    toolStripStatusLabelMessages.Image = Resources.information;
                    break;
                case MessageType.Warning:
                    MessageBox.Show(message, Settings.Default.AppFullName, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                    toolStripStatusLabelMessages.Image = Resources.warning;
                    break;
                case MessageType.Error:
                    toolStripStatusLabelMessages.Image = Resources.error;
                    break;
                case MessageType.Question:
                    result = MessageBox.Show(message, Global.AppFullName, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                    break;
            }

            // Return Result
            return result;
        }

        public void ChangeStatusSaveButton(bool value)
        {
            toolStripButtonSave.Enabled = toolStripMenuItemSave.Enabled = value;
        }
    }
}
